﻿namespace ApiTemplate.DAL
{
    public class SchedulerTaskDAL : BaseDAL, ISchedulerTaskDAL
    {
        /// <summary>
        /// 添加调度任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<string> AddAsync(AddSchedulerTaskInput input)
        {
            SchedulerTask schedulerTask = Mapper.Map<SchedulerTask>(input);
            schedulerTask.Id = Guid.NewGuid().ToString();
            await DbContext.Insertable(schedulerTask).ExecuteCommandAsync();
            return schedulerTask.Id;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(UpdateSchedulerTaskInput input)
        {
            SchedulerTask entity = Mapper.Map<SchedulerTask>(input);
            await DbContext.CopyNew().Updateable(entity)
                .IgnoreColumns(x => x.CreatedTime)
                .ExecuteCommandAsync();
            return true;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(BaseGuidListInput input)
        {
            await DbContext.Deleteable<SchedulerTaskLog>()
                .Where(x => input.Ids.Contains(x.TaskId))
                .ExecuteCommandAsync();
            await DbContext.Deleteable<SchedulerTask>()
                .Where(x => input.Ids.Contains(x.Id))
                .ExecuteCommandAsync();
            return true;
        }

        /// <summary>
        /// 获取单个任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<GetSchedulerTaskOutput> GetAsync(string id)
        {
            return await DbContext.CopyNew().Queryable<SchedulerTask>()
                .Select(x => new GetSchedulerTaskOutput())
                .FirstAsync(x => x.Id == id);
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<GetSchedulerTaskPageOutput>> GetPageResultAsync(GetSchedulerTaskPageInput input)
        {
            PageResult<GetSchedulerTaskPageOutput> result = new PageResult<GetSchedulerTaskPageOutput>();
            RefAsync<int> totalNumber = 0;
            result.Datas = await DbContext.Queryable<SchedulerTask>()
                .WhereIF(input.Status != -1, x => x.Status == input.Status)
                .WhereIF(input.RequsetType != -1, x => x.RequsetType == input.RequsetType)
                .WhereIF(!string.IsNullOrEmpty(input.Url), x => x.Url.Contains(input.Url))
                .WhereIF(!string.IsNullOrEmpty(input.TaskName), x => x.TaskName.Contains(input.TaskName))
                .Select(x => new GetSchedulerTaskPageOutput())
                .ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
            result.DataCount = totalNumber.Value;
            return result;
        }

        /// <summary>
        /// 获取所有任务
        /// </summary>
        /// <returns></returns>
        public List<GetSchedulerTaskOutput> GetAllSchedulerTaskList()
        {
            return DbContext.Queryable<SchedulerTask>()
                .Select(x => new GetSchedulerTaskOutput())
                .ToList();
        }

        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<bool> PauseTaskAsync(BaseGuidListInput input)
        {
            List<SchedulerTask> list = await DbContext.Queryable<SchedulerTask>()
                .Where(x => input.Ids.Contains(x.Id)).ToListAsync();
            foreach (var item in list)
            {
                item.Status = 1;
            }

            await DbContext.Updateable(list)
                .UpdateColumns("Status")
                .Where(x => input.Ids.Contains(x.Id))
                .ExecuteCommandAsync();

            return true;
        }

        /// <summary>
        /// 恢复任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<bool> RenewTaskAsync(BaseGuidListInput input)
        {
            List<SchedulerTask> list = await DbContext.Queryable<SchedulerTask>()
                .Where(x => input.Ids.Contains(x.Id)).ToListAsync();
            foreach (var item in list)
            {
                item.Status = 0;
            }

            await DbContext.Updateable(list)
                .UpdateColumns("Status")
                .Where(x => input.Ids.Contains(x.Id))
                .ExecuteCommandAsync();

            return true;
        }
    }
}
